﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LCASystem.ascx.cs" Inherits="C1Function" %>
<%@ Register TagPrefix="uc" TagName="Hotspot" Src="/Frontend/Controls/Hotspot.ascx" %>

<asp:PlaceHolder ID="plhErrorMessage" runat="server" Visible="false">
    <div class="alert alert-error">
        <strong>Error: </strong>
        <asp:Literal ID="litTopErrorMessage" runat="server"></asp:Literal>
    </div>
</asp:PlaceHolder>
<asp:PlaceHolder ID="plhSystemInfo" runat="server" Visible="false">
    <div class="alert alert-info">
        <strong>Information: </strong>
        <asp:Literal ID="litInfo" runat="server"></asp:Literal>
    </div>
</asp:PlaceHolder>
<asp:PlaceHolder ID="plhSystemDetails" runat="server" Visible="false">
    <div class="header">
        <div class="title pull-left">
            <h1>
                <asp:Literal ID="litTitle" runat="server"></asp:Literal>
            </h1>
        </div>
        <div class="picture pull-right">
            <div class="img">
                <asp:Literal runat="server" ID="litImg" />
            </div>
        </div>
        <div class="clearfix">
        </div>
    </div>
    <div class="system-container">
        <!-- Nav tabs -->
        <ul class="nav nav-tabs">
            <li class="active"><a href="#general" data-toggle="tab">General Data</a></li>
            <li class=""><a href="#input" data-toggle="tab">Input-Output</a></li>
            <li class=""><a href="#transportation" data-toggle="tab">Transportation</a></li>
            <li class=""><a href="#impact" data-toggle="tab">Impact Assessment</a></li>
        </ul>
        <!-- Tab panes -->
        <div class="tab-content">
            <div class="tab-pane fade in active" id="general">
                <div class="form-horizontal">
                    <div class="control-group">
                        <label class="control-label">
                            Description</label>
                        <div class="controls">
                            <asp:Literal runat="server" ID="litDescription" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">
                            Amount</label>
                        <div class="controls">
                            <asp:Literal runat="server" ID="litAmount" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">
                            Unit</label>
                        <div class="controls">
                            <asp:Literal runat="server" ID="litUnit" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">
                            Creation Date</label>
                        <div class="controls">
                            <asp:Literal runat="server" ID="litCreationDate" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">
                            Modified Date</label>
                        <div class="controls">
                            <asp:Literal runat="server" ID="litModifiedDate" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">
                        </label>
                        <div class="controls">
                            <asp:HyperLink runat="server" Target="_blank" ID="lnkProductionDiagram" CssClass="btn"
                                Text="Production Diagram" />
                        </div>
                    </div>
                </div>
            </div>
            <div class="tab-pane fade" id="input">
                <div class="chkboxes-inputoutput">
                    <label class="radio pull-left">
                        <input type="radio" name="optionsRadios" id="radInput" value="option1" />
                        <asp:Literal runat="server" ID="litInputChkTitle" />
                    </label>
                    <label class="radio pull-left">
                        <input type="radio" name="optionsRadios" id="radOutput" value="option2" />
                        <asp:Literal runat="server" ID="litOutputChkTitle" />
                    </label>
                    <div class="clearfix">
                    </div>
                </div>
                <div class="gridContents">
                    <asp:GridView CssClass="table table-condensed table-hover" runat="server" ID="gridInput"
                        DataKeyNames="Id" AutoGenerateColumns="False" GridLines="None" ClientIDMode="Static">
                        <Columns>
                            <asp:BoundField HeaderText="#" DataField="Count" SortExpression="Count"></asp:BoundField>
                            <asp:BoundField HeaderText="Item" DataField="Item" SortExpression="Item"></asp:BoundField>
                            <asp:BoundField HeaderText="Amount" DataField="Amount" SortExpression="Amount"></asp:BoundField>
                            <asp:BoundField HeaderText="Unit" DataField="Unit" SortExpression="Unit"></asp:BoundField>
                        </Columns>
                    </asp:GridView>
                    <asp:GridView CssClass="table table-condensed table-hover" runat="server" ID="gridOutput"
                        DataKeyNames="Id" AutoGenerateColumns="False" GridLines="None" ClientIDMode="Static">
                        <Columns>
                            <asp:BoundField HeaderText="#" DataField="Count" SortExpression="Count"></asp:BoundField>
                            <asp:BoundField HeaderText="Item" DataField="Item" SortExpression="Item"></asp:BoundField>
                            <asp:BoundField HeaderText="Amount" DataField="Amount" SortExpression="Amount"></asp:BoundField>
                            <asp:BoundField HeaderText="Unit" DataField="Unit" SortExpression="Unit"></asp:BoundField>
                        </Columns>
                    </asp:GridView>
                </div>
            </div>
            <div class="tab-pane fade" id="transportation">
                <div class="chkboxes-transportation">
                    <label class="radio pull-left">
                        <input type="radio" name="transportRadios"   id="radTransportA" value="option1" />
                        <asp:Literal runat="server" ID="litTransportAChkTitle" />
                    </label>
                    <label class="radio pull-left">
                        <input type="radio"  name="transportRadios"  id="radTransportB" value="option2" />
                        <asp:Literal runat="server" ID="litTransportBChkTitle" />
                    </label>
                    <div class="clearfix">
                    </div>
                </div>
                <div class="gridContents">
                    <asp:GridView CssClass="table table-condensed table-hover" runat="server" ID="gridTransportA"
                        DataKeyNames="Id" AutoGenerateColumns="False" GridLines="None" ClientIDMode="Static">
                        <Columns>
                            <asp:BoundField HeaderText="#" DataField="Count" SortExpression="Count"></asp:BoundField>
                            <asp:BoundField HeaderText="Flow" DataField="Item" SortExpression="Item"></asp:BoundField>
                            <asp:BoundField HeaderText="Amount" DataField="Amount" SortExpression="Amount"></asp:BoundField>
                            <asp:BoundField HeaderText="Unit" DataField="Unit" SortExpression="Unit"></asp:BoundField>
                        </Columns>
                    </asp:GridView>
                    <asp:GridView CssClass="table table-condensed table-hover" runat="server" ID="gridTransportB"
                        DataKeyNames="Id" AutoGenerateColumns="False" GridLines="None" ClientIDMode="Static">
                        <Columns>
                            <asp:BoundField HeaderText="#" DataField="Count" SortExpression="Count"></asp:BoundField>
                            <asp:BoundField HeaderText="Vehicle" DataField="Vehicle" SortExpression="Vehicle"></asp:BoundField>
                            <asp:BoundField HeaderText="Distance" DataField="Distance" SortExpression="Distance"></asp:BoundField>
                            <asp:BoundField HeaderText="Unit-Distance" DataField="UnitDistance" SortExpression="UnitDistance"></asp:BoundField>
                            <asp:BoundField HeaderText="Kg Load" DataField="KgLoad" SortExpression="KgLoad"></asp:BoundField>
                            <asp:BoundField HeaderText="Unit-Kg Load" DataField="UnitKgLoad" SortExpression="UnitKgLoad"></asp:BoundField>
                        </Columns>
                    </asp:GridView>
                </div>
            </div>
            <div class="tab-pane fade" id="impact">
                <div class="chkboxes-impact pull-left">
                    <label class="radio pull-left">
                        <input type="radio" name="impactRadios" id="radEco95" value="option1">
                        Eco-Indicator 95
                    </label>
                    <label class="radio pull-left">
                        <input type="radio" name="impactRadios" id="radCustom" value="option2">
                        Custom
                    </label>
                    <div class="clearfix">
                    </div>
                </div>
                <div class="recalculate pull-left">
                    <a id="lnkRecalculate" class="btn btn-primary">Recalculate Impact</a>
                </div>
                <div class="clearfix">
                </div>
                <div class="gridContents">
                    <div id="plsClickInfo" class="alert alert-info">
                        <strong>Information: </strong>Please click <strong>Recalculate Impact</strong> to
                        see or update the impact assessment
                    </div>
                    <div>
                        <div class="loading">
                            <img src="/Admin/Images/ajax-loader.gif" />
                            Loading...
                        </div>
                        <table border="0" cellspacing="0" style="border-collapse: collapse; display: table;"
                            id="gridEco95" class="table table-condensed table-hover">
                            <thead>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                        <table border="0" cellspacing="0" style="border-collapse: collapse; display: table;"
                            id="gridCustom" class="table table-condensed table-hover">
                            <thead>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                        <%--<table border="0" cellspacing="0" style="border-collapse: collapse; display: table;"
                            id="gridEco95" class="table table-condensed table-hover">
                            <thead>
                                <tr>
                                    <th scope="col">
                                        #
                                    </th>
                                    <th scope="col">
                                        Factor
                                    </th>
                                    <th scope="col">
                                        Impact
                                    </th>
                                    <th scope="col">
                                        Unit
                                    </th>
                                    <th scope="col">
                                        &nbsp;
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>
                                        1
                                    </td>
                                    <td>
                                        Greenhouse
                                    </td>
                                    <td>
                                        21.33
                                    </td>
                                    <td>
                                        Kg CO2
                                    </td>
                                    <td>
                                        <a class="btn btn-mini lnkHotspot" id="lnkHotspot">Hotspot</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        2
                                    </td>
                                    <td>
                                        Heavy Metals
                                    </td>
                                    <td>
                                        21.33
                                    </td>
                                    <td>
                                        Kg Pb
                                    </td>
                                    <td>
                                        <a class="btn btn-mini lnkHotspot" id="lnkHotspot">Hotspot</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        3
                                    </td>
                                    <td>
                                        Eutrophication Potential
                                    </td>
                                    <td>
                                        21.33
                                    </td>
                                    <td>
                                        Kg PO4
                                    </td>
                                    <td>
                                        <a class="btn btn-mini lnkHotspot" id="lnkHotspot">Hotspot</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        4
                                    </td>
                                    <td>
                                        Energy Resource Depletion
                                    </td>
                                    <td>
                                        21.33
                                    </td>
                                    <td>
                                        MJ LHV
                                    </td>
                                    <td>
                                        <a class="btn btn-mini lnkHotspot" id="lnkHotspot">Hotspot</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        5
                                    </td>
                                    <td>
                                        Ozone Layer Depletion
                                    </td>
                                    <td>
                                        21.33
                                    </td>
                                    <td>
                                        Kg CFC11
                                    </td>
                                    <td>
                                        <a class="btn btn-mini lnkHotspot" id="lnkHotspot">Hotspot</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        6
                                    </td>
                                    <td>
                                        Acidification Potential
                                    </td>
                                    <td>
                                        21.33
                                    </td>
                                    <td>
                                        Kg SO2
                                    </td>
                                    <td>
                                        <a class="btn btn-mini lnkHotspot" id="lnkHotspot">Hotspot</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        7
                                    </td>
                                    <td>
                                        Solid Waste
                                    </td>
                                    <td>
                                        21.33
                                    </td>
                                    <td>
                                        Kg
                                    </td>
                                    <td>
                                        <a class="btn btn-mini lnkHotspot" id="lnkHotspot">Hotspot</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        8
                                    </td>
                                    <td>
                                        Carcinogens
                                    </td>
                                    <td>
                                        21.33
                                    </td>
                                    <td>
                                        Kg B(a)P
                                    </td>
                                    <td>
                                        <a class="btn btn-mini lnkHotspot" id="lnkHotspot">Hotspot</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        9
                                    </td>
                                    <td>
                                        Pesticides
                                    </td>
                                    <td>
                                        21.33
                                    </td>
                                    <td>
                                        -
                                    </td>
                                    <td>
                                        <a class="btn btn-mini lnkHotspot" id="lnkHotspot">Hotspot</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        10
                                    </td>
                                    <td>
                                        Summer Smog
                                    </td>
                                    <td>
                                        21.33
                                    </td>
                                    <td>
                                        Kg SPM
                                    </td>
                                    <td>
                                        <a class="btn btn-mini lnkHotspot" id="lnkHotspot">Hotspot</a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>--%>
                    </div>
                    <%--                    <asp:GridView CssClass="table table-condensed table-hover" runat="server" ID="gridEco95"
                        DataKeyNames="Id" AutoGenerateColumns="False" GridLines="None" ClientIDMode="Static">
                        <Columns>
                            <asp:BoundField HeaderText="#" DataField="Count" SortExpression="Count"></asp:BoundField>
                            <asp:BoundField HeaderText="Factor" DataField="Factor" SortExpression="Factor"></asp:BoundField>
                            <asp:BoundField HeaderText="Impact" DataField="Impact" SortExpression="Impact"></asp:BoundField>
                            <asp:BoundField HeaderText="Unit" DataField="Unit" SortExpression="Unit"></asp:BoundField>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:HyperLink ID="lnkHotspot" CssClass="btn btn-mini lnkHotspot" Text="Hotspot" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>--%>
                </div>
            </div>
        </div>
        <div class="id hide"> 
            <asp:Literal runat="server" ID="litSystemId" />
        </div>
    </div>
    <uc:Hotspot runat="server" ID="ctlHotspot" />
    
</asp:PlaceHolder>
<script type="text/javascript">
    var hotspots = null;

    $(document).ready(function () {
        initLCASystem();
        

    });
    ///// Input - Output
    $('input#radInput').on("click", function (e, data) {
        $("table#gridOutput").hide();
        $("table#gridInput").hide();

        $("table#gridInput").fadeIn(200);
    });

    $('input#radOutput').on("click", function (e, data) {
        $("table#gridOutput").hide();
        $("table#gridInput").hide();

        $("table#gridOutput").fadeIn(200);

    });

    ///// Transportation   
    $('input#radTransportA').on("click", function (e, data) {
        $("table#gridTransportA").hide();
        $("table#gridTransportB").hide();

        $("table#gridTransportA").fadeIn(200);

    });

    $('input#radTransportB').on("click", function (e, data) {
        $("table#gridTransportA").hide();
        $("table#gridTransportB").hide();

        $("table#gridTransportB").fadeIn(200);

    });


    ///// Impact Assessment
    $('input#radEco95').on("click", function (e, data) {
        $("table#gridEco95").hide();
        $("table#gridCustom").hide();

        $("table#gridEco95").fadeIn(200);

    });

    $('input#radCustom').on("click", function (e, data) {
        $("table#gridEco95").hide();
        $("table#gridCustom").hide();

        $("table#gridCustom").fadeIn(200);

    });
    $('a#lnkRecalculate').on("click", function (e, data) {
        $("#plsClickInfo").hide();
        $('table#gridEco95').hide();

        getImpactFactorCalculation();
    });

    function initLCASystem() {
        $('input#radInput').click();
        $('input#radTransportA').click();
        $("#impact .gridContents .loading").invisible();
    }


     


    function getImpactFactorCalculation() {
        var systemId = $.trim($(".system-container .id").text());
        var req = { 
            opt: "calculate",
            systemId: systemId
        };
        
        $("#impact .gridContents .loading").visible();

        $.ajax({
            type: 'POST',
            url: '/Services/ImpactFactorCalculationService.aspx',
            data: JSON.stringify(req),
            contentType: 'application/json; charset=utf-8',
            success: function (request, msg) {

                var obj = JSON.parse(request);
                // var decoded = $('<div/>').html(obj.Data).text();

                console.log('success: ' + obj.Result);
                if (obj.Result)
                    updateTable(obj.Data);
                else
                    showErrorMessage(obj.Data);

                var raw = JSON.parse(obj.Hotspot);
                hotspots = new LCA.Hotspot();
                hotspots.init(raw);

                setTimeout('$("#impact .gridContents .loading").invisible();', 1000);


            },
            error: function (request, status, error) {
                console.log('error');
                showErrorMessage(error);
                setTimeout('$("#impact .gridContents .loading").invisible();', 1000);
            }

        }); 

    }

    function showErrorMessage(msg) {
        console.log('error: '+msg);
    
    }

    function updateTable(data)
    {
        data = JSON.parse(data);

        loadTable('gridEco95', data);
        loadTable('gridCustom', data);

        // Click radio95
        $('input#radEco95').click();
    }

    function loadTable(tableId, data) {

        $('#impact .gridContents table#' + tableId).empty();

        var oTable = $('#impact .gridContents table#' + tableId).dataTable({
            "bProcessing": false,
            "bDestroy": true,
            "bPaginate": false,
            "bAutoWidth": false,
            "bFilter": false,
            "bInfo": false,
            "aaSorting": [],
            "aaData": data,
            "aoColumns": [

                    { "mDataProp": "FactorName", "bSortable": true, "sTitle": "Name", "sClass": "cName", "bSearchable": true, "bVisible": true },
                    { "mDataProp": "Impact", "bSortable": false, "sTitle": "Impact", "sClass": "cName", "bSearchable": true, "bVisible": true },
                    { "mDataProp": "UnitName", "bSortable": false, "sTitle": "Unit", "sClass": "cUnit", "bSearchable": true, "bVisible": true },
                    { "mDataProp": null, "bSortable": false, "sTitle": "", "sClass": "cDelete", "sDefaultContent": "<a class='btn btn-mini lnkHotspot' id='lnkHotspot'>Hotspot</a>", "bVisible": true }
                ],
            "fnCreatedRow": function (nRow, aData, iDataIndex) {

                console.log('FactorId: ' + aData.FactorId);

                if (tableId == 'gridEco95') {
                    if (aData.IndicatorId == LCA.Indicator.CUSTOM)
                        $(nRow).hide();
                }

                if (tableId == 'gridCustom') {
                    if (aData.IndicatorId == LCA.Indicator.ECO_INDICATOR_95)
                        $(nRow).hide();
                }


                // IndicatorId
                $('a.lnkHotspot', nRow).data('FactorId', aData.FactorId);
                $('a.lnkHotspot', nRow).data('Impact', aData.Impact);

                $('a.lnkHotspot', nRow).click(function (e) {
                    var factorId = $(this).data('FactorId');
                    var impact = $(this).data('Impact');

                    // Hotspot.ascx
                    loadHotspotModal(factorId, impact);
                });


            },
            "fnInitComplete": function (oSettings, json) {
                // Recheck again if 'fnInitComplete' is the right method to call


            }


        });

    }



    
    
</script>
